package com.common.base.widget

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.common.base.R


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class SImageText @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) :
    RelativeLayout(context, attrs, defStyle) {
     var iv: ImageView? = null
     var tv: TextView? = null
    private var isChecked = false
    private var imgResId = 0
    private var checkedImgResId = 0

    init {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        val typed = context.obtainStyledAttributes(
            attrs,
            R.styleable.SImageText
        )

        // 图片资源
        val imgResId = typed.getResourceId(
            R.styleable.SImageText_itImage,
            ATTR_NOT_FOUND
        )

        // 图片宽度
        val imgWidth = typed.getDimensionPixelSize(
            R.styleable.SImageText_itImageWidth, LayoutParams.WRAP_CONTENT
        )

        // 图片高度
        val imgHeight = typed.getDimensionPixelSize(
            R.styleable.SImageText_itImageHeight, LayoutParams.WRAP_CONTENT
        )

        // 图片方位
        val imgLocation = typed.getInt(
            R.styleable.SImageText_itImageLocation,
            LOCATION_LEFT
        )

        // 文字
        val text = typed.getString(R.styleable.SImageText_itText)

        // 文字颜色资源
        val textColorResId = typed.getResourceId(
            R.styleable.SImageText_itTextColor, ATTR_NOT_FOUND
        )

        // 文字尺寸
        val textSize = typed.getDimensionPixelSize(
            R.styleable.SImageText_itTextSize,
            ATTR_NOT_FOUND
        ).toFloat()

        // 文字样式 正常|加粗
        val textStyle = typed.getInt(
            R.styleable.SImageText_itTextStyle,
            TEXT_STYLE_NORMAL
        )

        // 图文间隔
        val space = typed.getDimensionPixelSize(
            R.styleable.SImageText_itSpace,
            SPACE
        )

        // 图片填充样式位
        val imageScaleType = typed.getInt(
            R.styleable.SImageText_itImageScaleType,
            LOCATION_LEFT
        )
        typed.recycle()

        // 构建图片控件
        iv = ImageView(context)
        iv?.id = IMAGE_ID
        when (imageScaleType) {
            1 -> iv?.scaleType = ImageView.ScaleType.FIT_XY
            2 -> iv?.scaleType = ImageView.ScaleType.FIT_CENTER
            else -> iv?.scaleType = ImageView.ScaleType.CENTER_INSIDE
        }
        if (imgResId != ATTR_NOT_FOUND) {
            iv?.setImageResource(imgResId)
            this.imgResId = imgResId
        }
        val imageParams = LayoutParams(imgWidth, imgHeight)

        // 构建文字控件
        tv = TextView(context)
        val textParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        tv?.text = text
        tv?.id = TEXT_ID
        tv?.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
        if (textColorResId != ATTR_NOT_FOUND) {
            tv?.setTextColor(context.resources.getColor(textColorResId))
        }
        if (textStyle == TEXT_STYLE_BOLD) {
            tv?.paint?.isFakeBoldText = true
        }
        when (imgLocation) {
            LOCATION_LEFT -> {
                imageParams.addRule(ALIGN_PARENT_LEFT)
                imageParams.addRule(CENTER_VERTICAL)
                textParams.addRule(RIGHT_OF, IMAGE_ID)
                textParams.addRule(CENTER_VERTICAL)
                textParams.leftMargin = space
            }

            LOCATION_TOP -> {
                imageParams.addRule(ALIGN_PARENT_TOP)
                imageParams.addRule(CENTER_HORIZONTAL)
                textParams.addRule(BELOW, IMAGE_ID)
                textParams.addRule(CENTER_HORIZONTAL)
                textParams.topMargin = space
            }

            LOCATION_RIGHT -> {
                textParams.addRule(ALIGN_PARENT_LEFT)
                textParams.addRule(CENTER_VERTICAL)
                imageParams.addRule(ALIGN_RIGHT, IMAGE_ID)
                imageParams.addRule(CENTER_VERTICAL)
                imageParams.leftMargin = space
            }

            LOCATION_BOTTOM -> {
                textParams.addRule(ALIGN_PARENT_TOP)
                textParams.addRule(CENTER_HORIZONTAL)
                imageParams.addRule(BELOW, IMAGE_ID)
                imageParams.addRule(CENTER_HORIZONTAL)
                imageParams.topMargin = space
            }
        }
        this.addView(iv, imageParams)
        this.addView(tv, textParams)
    }

    fun setText(text: String?) {
        tv?.text = text
    }

    fun setText(resId: Int) {
        tv?.setText(resId)
    }

    fun setImage(resId: Int) {
        iv?.setImageResource(resId)
    }

    fun setCheckedImgResId(resId: Int) {
        checkedImgResId = resId
    }

    fun setChecked(isChecked: Boolean) {
        if (isChecked) {
            checked()
        } else {
            unChecked()
        }
    }

    fun checked() {
        isChecked = true
        setImage(checkedImgResId)
    }

    fun unChecked() {
        isChecked = false
        setImage(imgResId)
    }

    fun toggle(): Boolean {
        isChecked = !isChecked
        setChecked(isChecked)
        return isChecked
    }

    companion object {
        /**
         * 图片方位 左上右下
         */
        const val LOCATION_LEFT = 0
        const val LOCATION_TOP = 1
        const val LOCATION_RIGHT = 2
        const val LOCATION_BOTTOM = 3
        const val IMAGE_ID = 101
        const val TEXT_ID = 102

        /**
         * 文字样式 正常|加粗
         */
        const val TEXT_STYLE_NORMAL = 0
        const val TEXT_STYLE_BOLD = 1

        /**
         * 表示未找到的属性值
         */
        const val ATTR_NOT_FOUND = -1

        /**
         * 默认图文间隔
         */
        const val SPACE = 5
    }
}